<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.infoStatus" multiple placeholder="请选择信息状态">
          <el-option
            v-for="item in infoStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="infoId"
        header-align="center"
        align="center"
        label="信息Id">
      </el-table-column>
      <el-table-column
        prop="catId"
        header-align="center"
        align="center"
        label="所属分类">
      </el-table-column>
      <el-table-column
        prop="infoTitle"
        header-align="center"
        align="center"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="infoDefaultImg"
        header-align="center"
        align="center"
        label="默认图片">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.infoDefaultImg !== ''"
            style="width: 75px; height: 75px"
            :src="scope.row.infoDefaultImg"
            fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="infoStatus"
        header-align="center"
        align="center"
        label="信息状态[0-待,1-审核,2-成功,-1-删,-2-失败]">
      </el-table-column>
      <el-table-column
        prop="anon"
        header-align="center"
        align="center"
        label="匿名发布[0-否,1-是]">
        <template slot-scope="scope">
          <span v-if="scope.row.anon === 1" >是</span>
          <span v-else >否</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="上传者id">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showInfoAttrValue(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageData.currPage"
      :page-sizes="[5]"
      :page-size="pageData.pageSize"
      :total="pageData.totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <infoAttrValue v-if="infoAttrValueVisible" ref="infoAttrValue" @checkEvent="handlerCheckEvent"></infoAttrValue>
    <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
  </div>
</template>

<script>
  import InfoAttrValue from './info-attr-value'
  export default {
    data () {
      return {
        infoId: 0,
        dataForm: {
          key: '',
          infoStatus:[],
        },
        dataList: [],
        pageData:{
          totalCount: 0,
          pageSize: 5,
          currPage: 1,
          totalPage: 0,
        },
        dataListLoading: false,
        infoAttrValueVisible: false,
        infoStatusOptions: [{
          value: 2,
          label: '审核通过'
        }, {
          value: 1,
          label: '待审核'
        }, {
          value: 0,
          label: '草稿'
        }, {
          value: -1,
          label: '已删除'
        }, {
          value: -2,
          label: '审核不通过'
        }],
      }
    },
    components: {
      InfoAttrValue,
    },
    activated () {
      // this.getDataList()
      // 信息状态[0-待,1-审核,2-成功,-1-删,-2-失败]
      this.dataList=[
        {
          infoId:1,
          catId:5,
          infoTitle:'拼车去威海站',
          infoDefaultImg:'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
          infoStatus:1,
          anon:0,
          userId: 1,
          nickName:"老张",
          createTime:'2021/05/02 20:07:08',
          comment_num:0,
        },{
          infoId:4,
          catId:8,
          infoTitle:'丢失校园卡',
          infoDefaultImg:'',
          infoStatus:2,
          anon:0,
          userId: 2,
          nickName:"老朱",
          createTime:'2021/05/25 20:08:58',
          comment_num:5,
        }
      ]
    },
    methods: {
      // 获取数据列表
      getDataList () {
        console.log("获取信息", this.dataForm.infoStatus)
        var data =  {
          page:{
            page: ''+this.pageData.currPage,
            limit: ''+this.pageData.pageSize,
            sidx: 'create_time',
            order: 'asc', 
            key: this.dataForm.key,
          },
          infoStatus: this.dataForm.infoStatus,
          // userId: this.currentUser.user.userId,
          userId: '0',
          useUserId: "-1",
          // catId: this.searchInfo.catId === 0? '' : ''+this.searchInfo.catId,
        }
        // return;
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/admin/info/list'),
          method: 'post',
          data: this.$http.adornData(data, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.pageData.totalCount = data.page.totalCount
            this.pageData.totalPage = data.page.totalPage
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageData.pageSize = val
        this.pageData.currPage = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageData.currPage = val
        this.getDataList()
      },
      // 展示信息详情
      showInfoAttrValue(row){
        this.infoId = row.infoId
        this.infoAttrValueVisible = true;
        this.$nextTick(() => {
          this.$refs.infoAttrValue.init(row.infoId)
        })
      },
      // 检测到 审核事件
      handlerCheckEvent(infoId, pass){
        // 前端修改
        var index = this.getIndexFromDataList(infoId);
        if(index === -1){
          return;
        }
        this.dataList[index].infoStatus = pass;
        // 后端请求
        

      },
      getIndexFromDataList(infoId){
        let _dataList = this.dataList;
        for(var i = 0;i<_dataList.length;i++){
          if(_dataList[i].infoId === infoId){
            return i;
          }
        }
        return -1;
      }
      
    }
  }
</script>
